import 'package:device_info/device_info.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/log_utils.dart';

class LoginLogic extends GetxController {
  String DeviceName = "";
  static LoginLogic get to => Get.put(LoginLogic());

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    ///获取设备型号
    _getDeviceInfo();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    Log.d('控制器销毁---');
  }


  ///用户信息
  void getUserInfoData() async {
    if (!UserController.to.isLogin) {
      return;
    }
    Log.d('获取用户信息接口');
    try {
      var user = await ServiceRepository.user();
      UserController.to.saveUser(user);
    } catch (e, s) {
      Log.d(http.setError(e, s));
    }
  }

  ///发送验证码
  Future<bool> shayuSms(mobile,String smsCode) async {
    EasyLoading.show(status: '发送中...');
    try {
      await ServiceRepository.shayuSms(mobile,smsCode);
      EasyLoading.showToast('发送成功');
      return true;
    } catch (e, s) {
      EasyLoading.showToast('发送失败');
      return false;
    }
  }

  ///注册
  void register({String? username,String? code,String? password,String? referee,}) async {
    EasyLoading.show(status: '加载中...');
    try {
      var user = await ServiceRepository.register(username: username,code: code,password: password,referee: referee,);
      EasyLoading.showToast('注册成功');
      UserController.to.saveToken(user);
      EasyLoading.showToast('登录成功');
      Future.delayed(Duration(milliseconds: 200), () {
        Get.back();
        Get.back();
      });
    } catch (e, s) {
      EasyLoading.showToast('注册失败');
    }
  }

  ///手机登陆
  void login({required String username,required String password}) async {
    EasyLoading.show(status: '加载中...');
    Log.d("获取设备信息---"+DeviceName);
    try {
      var user = await ServiceRepository.login(username:username, password:password,mobile_mark:DeviceName);
      UserController.to.saveToken(user);
      EasyLoading.showToast('登录成功');
      Future.delayed(Duration(milliseconds: 200), () {
        Get.back();
      });
    } catch (e, s) {
      EasyLoading.showToast(http.setError(e,s));
    }
  }

  ///重制密码
  void changePassword(loginId, password,repassword,verify_code,type) async {
    try {
      await ServiceRepository.changePassword(loginId:loginId, password:password,repassword:repassword,verify_code:verify_code,type: type);
      EasyLoading.showToast('修改成功');
      Future.delayed(Duration(milliseconds: 200), () {
        Get.back();
      });
    } catch (e, s) {
      EasyLoading.showToast(http.setError(e,s));
    }
  }

  ///获取设备型号
  void _getDeviceInfo() async{

    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

    if(Global.isAndroid){
      // 安卓系统
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      DeviceName = androidInfo.model;
      print('设备唯一标识: ${androidInfo}');
      // 更多信息请查看 AndroidDeviceInfo 类中的定义

    }else if(Global.isIOS){
      // 苹果系统
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      DeviceName = iosInfo.name;
      print('设备唯一标识：${iosInfo}');
      // 更多信息请查看 IosDeviceInfo 类中的定义
    }
  }
}
